#!/usr/bin/env python
'''
Copyright (C) 2006, 2007 Aaron Spike, aaron@ekips.org

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'''

import pygame, random
from pygame.locals import *
from activity import *

themes = [
    {'mouse':'mouse.png','cheese':'cheese.png','mouth':'bottom','shrink':0},
    {'mouse':'mouse_cz.png','cheese':'cheese_cz.png','mouth':'top','shrink':-20}
    ]
THEME = themes[1]
if len(sys.argv) > 1:
    THEME = themes[0]
MAKECHEESE = USEREVENT + 1

class Mouse(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) 
        self.image, self.rect = load_image(THEME['mouse'])
        self.mouth = pygame.Rect((0,0),(80,100))
        self.yums = [load_sound('yum1.ogg'),load_sound('yum2.ogg'),load_sound('slurp.ogg')]
    def update(self):
        pos = pygame.mouse.get_pos()
        if THEME['mouth'] == 'bottom':
            self.rect.midbottom = pos
            self.mouth.midbottom = self.rect.midbottom
        else:
            self.rect.midtop = pos
            self.mouth.midtop = self.rect.midtop
    def eat(self,group):
        slurp = False
        for x in group.sprites():
            if self.mouth.colliderect(x.rect.inflate(THEME['shrink'],THEME['shrink'])):
                x.remove(group)
                slurp = True
        if slurp:
            random.choice(self.yums).play()

class Cheese(pygame.sprite.Sprite):
    def __init__(self, screen):
        pygame.sprite.Sprite.__init__(self) 
        self.image, self.rect = load_image(THEME['cheese'])
        self.pos = (-self.rect.width,-self.rect.height)
        
        self.x, self.y = screen.get_size()
        self.x = self.x - self.rect.width
        self.y = self.y - self.rect.height
    def update(self):
        self.rect.center = self.pos
    def change(self):
        x = int((random.random()*self.x)+(self.rect.width/2.0))
        y = int((random.random()*self.y)+(self.rect.height/2.0))
        self.pos = (x,y)

class CheeseActivity(Activity):
    def __init__(self, screen):
        Activity.__init__(self, screen)
        self.mousey = Mouse()
        self.cheese = Cheese(self.screen)
        self.mouse = pygame.sprite.RenderPlain((self.mousey))
        self.cheeses = pygame.sprite.RenderPlain()
        self.pos = None
        
    def setup(self):
        pygame.time.set_timer(MAKECHEESE,3000)
        
    def setup_background(self):
        self.background.fill((200,200,200))
        
    def handle_events(self):
        event = pygame.event.wait()
        if event.type == QUIT:
            self.quit = True
            return
        elif event.type == KEYUP:
            if event.key == K_ESCAPE:
                self.quit = True
                return
        elif event.type == MAKECHEESE:
            if not self.cheese.alive():
                self.cheese.change()
                self.cheese.add(self.cheeses)
                self.changed = True
        self.pos = pygame.mouse.get_pos()
        if self.pos != self.mprev:
            self.changed = True
            
    def on_change(self):
        self.mouse.update()
        self.cheeses.update()
        self.mousey.eat(self.cheeses)
        
        self.cheeses.draw(self.screen)
        self.mouse.draw(self.screen)
        
        self.mprev = self.pos
        
            